package com.dhemery.mack.adapters;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IPersistableElement;

import com.dhemery.mack.model.Folder;
import com.dhemery.mack.model.Node;
import com.dhemery.mack.model.Session;

public class NodeEditorInput implements IEditorInput {
	private final Node node;
	private final ImageDescriptor imageDescriptor;

	public NodeEditorInput(Node node, ImageDescriptor imageDescriptor) {
		this.node = node;
		this.imageDescriptor = imageDescriptor;
	}

	// TODO Any way to test this?
	public Image createImage() {
		return imageDescriptor.createImage();
	}

	public boolean equals(Object other) {
		if (!(other instanceof NodeEditorInput))
			return false;
		return node == ((NodeEditorInput) other).node;
	}

	public boolean exists() {
		return false;
	}

	@SuppressWarnings("unchecked")
	public Object getAdapter(Class adapter) {
		return null;
	}

	public String getDescription() {
		return node.getDescription();
	}

	public ImageDescriptor getImageDescriptor() {
		return imageDescriptor;
	}

	public String getName() {
		return node.getName();
	}

	private String getPath(Node node) {
		if (node instanceof Session) return "";
		return getPath(node.getFolder()) + getPathStep(node);
	}

	private String getPathStep(Node node) {
		return "{" + getPositionInFolder(node) + ":" + node.getName() + "}";
	}

	public IPersistableElement getPersistable() {
		return null;
	}

	private int getPositionInFolder(Node node) {
		Folder folder = node.getFolder();
		return folder.getNodes().indexOf(node) + 1;
	}

	public String getToolTipText() {
		return getPath(node);
	}

	public void setDescription(String newDescription) {
		node.setDescription(newDescription);
	}

	public void setName(String newName) {
		node.setName(newName);
	}
}
